using AutoMapper;
using B.S.BaseData.Domain;
using MediatR;
using SmartTracking.API.Read.Application.Commands.ZXL.Eliminater;
using SmartTracking.ErrorCode;
using SmartTracking.Infrastructure.Interfaces;

namespace SmartTracking.API.Read.Application.CommandHandlers.ZXL.Eliminater
{
    public class GetEliminateCommandHandler : IRequestHandler<GetEliminateCommand, ApiPaging<Eliminate>>
    {

        private readonly IBaseRepository<Eliminate> eliminate;
        private readonly IMapper map;

        public GetEliminateCommandHandler(IBaseRepository<Eliminate> eliminate, IMapper map)
        {
            this.eliminate = eliminate;
            this.map = map;
        }

        public Task<ApiPaging<Eliminate>> Handle(GetEliminateCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<Eliminate> page = new ApiPaging<Eliminate>();

            var linq = eliminate.GetAll().Where(x=>x.IsDeleted == false);

            page.totalCount = linq.Count();
            page.totalPage = (int)Math.Ceiling(page.totalCount * 1.0 / request.PageSize);

            linq = linq.OrderBy(x=>x.EliminateId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);

            page.data = linq.ToList();

            return Task.FromResult(page);

        }
    }
}